package com.chain.BrotherExchange.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chain.BrotherExchange.R;
import com.chain.BrotherExchange.data.DataRepository;
import com.chain.BrotherExchange.data.model.ApplyUnlcokList;
import com.chain.BrotherExchange.ui.core.AbsNetworkStateActivity;
import com.chain.BrotherExchange.utils.ActivityBarHelper;
import com.chain.BrotherExchange.utils.Androids;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.List;

import butterknife.Bind;

/**
 * 解仓列表
 */
public class UnlockedPositionListActivity extends AbsNetworkStateActivity implements OnRefreshLoadmoreListener {

    @Bind(R.id.recycler_view) RecyclerView mRecyclerView;
    @Bind(R.id.smart_refresh_layout) SmartRefreshLayout mSmartRefreshLayout;

    private int mCurrentPage = 1;
    private UnlockedAdapter mAdapter;

    public static void launch(Context context){
        context.startActivity(new Intent(context, UnlockedPositionListActivity.class));
    }

    @Override
    public int getTitleLayoutRes() {
        return R.layout.layout_default_title_bar;
    }

    @Override
    protected int getLayoutID() {
        return R.layout.simple_load_refresh_recycler_view;
    }

    @Override
    protected void initActions() {
        ActivityBarHelper.init(this, R.string.unlocked_list);

        mSmartRefreshLayout.setOnRefreshLoadmoreListener(this);
        mAdapter = new UnlockedAdapter();
        mRecyclerView.setAdapter(mAdapter);

        // 请求数据
        initUnlockedPositionData();
    }

    private void initUnlockedPositionData(){
        loadingNetwork();
        mCurrentPage = 1;
        String token = Androids.LocalConfig.getToken();
        int uid = Androids.LocalConfig.getUid();

        DataRepository.get().getApplyUnlockList(uid, token, mCurrentPage, result -> {
            mSmartRefreshLayout.finishRefresh();
            successNetwork();

            if (result != null && result.isSucceed()) {
                List<ApplyUnlcokList.DataBean.ListBean.ItemsBean> items = result.getData()
                        .getList().getItems();
                if(!items.isEmpty()){
                    mAdapter.setData(items);
                } else {
                    emptyNetwork();
                }

            }  else if(result != null){
                errorNetwork(result.getMsg());
            }
        });
    }

    private void loadMoreUnlockedPositionData(){
        String token = Androids.LocalConfig.getToken();
        int uid = Androids.LocalConfig.getUid();

        DataRepository.get().getApplyUnlockList(uid, token, ++mCurrentPage, result -> {
            mSmartRefreshLayout.finishLoadmore();
            if (result != null && result.isSucceed()) {
                List<ApplyUnlcokList.DataBean.ListBean.ItemsBean> items = result.getData()
                        .getList().getItems();
                if(!items.isEmpty()){
                    mAdapter.addData(items);
                }
            }
        });
    }

    @Override
    public void onAnewRequest() {
        initUnlockedPositionData();
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        loadMoreUnlockedPositionData();
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        initUnlockedPositionData();
    }


    static class UnlockedAdapter extends RecyclerView.Adapter<UnlockedAdapter.ViewHolder>{

        private List<ApplyUnlcokList.DataBean.ListBean.ItemsBean> list;
        private int uid = Androids.LocalConfig.getUid();
        private String token = Androids.LocalConfig.getToken();

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
            return new ViewHolder(LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_unlcoked, parent, false));
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int i) {
            ApplyUnlcokList.DataBean.ListBean.ItemsBean itemsBean = list.get(i);

            int status = itemsBean.getStatus();
            holder.mNumberText.setText(itemsBean.getTotal());
            holder.mDateText.setText(itemsBean.getCreate_time());
            holder.mStateText.setText(itemsBean.getStatusText());

            if(status == 1){
                holder.mActionText.setVisibility(View.VISIBLE);
                holder.mActionText.setOnClickListener(v -> cancelUnlock(itemsBean));
            } else {
                holder.mActionText.setVisibility(View.GONE);
            }
        }

        private void cancelUnlock(ApplyUnlcokList.DataBean.ListBean.ItemsBean item) {
            DataRepository.get().cancelApplyToken(uid, token, item.getId(), result -> {
                if (result != null && result.isSucceed()) {
                    Androids.makeText(result.getMsg());
                    item.setStatus(3);
                    notifyDataSetChanged();
                }
            });
        }

        @Override
        public int getItemCount() {
            return list != null ? list.size() : 0;
        }

        public void setData(List<ApplyUnlcokList.DataBean.ListBean.ItemsBean> items){
            this.list = items;
            notifyDataSetChanged();
        }

        public void addData(List<ApplyUnlcokList.DataBean.ListBean.ItemsBean> items){
            this.list.addAll(items);
            notifyDataSetChanged();
        }

        static class ViewHolder extends RecyclerView.ViewHolder{

            private TextView mNumberText, mStateText, mDateText, mActionText;

            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                mNumberText = itemView.findViewById(R.id.number_text);
                mStateText = itemView.findViewById(R.id.state_text);
                mDateText = itemView.findViewById(R.id.date_text);
                mActionText = itemView.findViewById(R.id.action_text);
            }
        }
    }
}
